
<template>
  <div>
    <div class="gva-search-box">
      <el-form
        ref="elSearchFormRef"
        :inline="true"
        :model="searchInfo"
        class="demo-form-inline"
        :rules="searchRule"
        @keyup.enter="onSubmit"
      >
        <el-form-item>
          <el-input
            v-model="searchInfo.game_name"
            placeholder="游戏名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="searchInfo.type"
            placeholder="游戏类型"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchInfo.develop_type"
            placeholder="类型"
            clearable
            style="width: 150px"
          >
            <el-option
              label="自研"
              :value="1"
            />
            <el-option
              label="非自研"
              :value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchInfo.cp_name"
            placeholder="游戏厂商"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchInfo.origin_name"
            placeholder="原包名"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="search"
            @click="onSubmit"
          >查询</el-button>
          <el-button
            icon="refresh"
            @click="onReset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <el-button
          type="primary"
          icon="plus"
          @click="openDialog"
        >新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        border
      >
        <el-table-column
          align="left"
          label="游戏名称"
          prop="game_name"
        />
        <el-table-column
          align="left"
          label="游戏类型"
          prop="type"
        />
        <el-table-column
          align="left"
          label="类型"
        >
          <template #default="scope">
            {{ scope.row.develop_type == 1 ? '自研' : '非自研' }}
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="游戏厂商"
          prop="cp_name"
        >
          <template #default="scope">
            {{ scope.row.develop_type == 2 ? scope.row.cp_name : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="原包名"
          prop="origin_name"
        />
        <el-table-column
          align="left"
          label="操作"
          fixed="right"
          width="160"
        >
          <template #default="scope">
            <el-button
              type="primary"
              link
              icon="edit"
              class="table-button"
              @click="updateQmGameFunc(scope.row)"
            >变更</el-button>
            <el-button
              type="primary"
              link
              icon="delete"
              @click="deleteRow(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          layout="total, prev, pager, next, jumper"
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogFormVisible"
      :before-close="closeDialog"
      :title="type==='create'?'添加':'修改'"
      destroy-on-close
    >
      <el-scrollbar height="400px">
        <el-form
          ref="elFormRef"
          :model="formData"
          label-position="right"
          :rules="rule"
          label-width="120px"
        >
          <el-form-item
            label="游戏名称:"
            prop="game_name"
          >
            <el-input
              v-model="formData.game_name"
              :clearable="true"
              placeholder="请输入游戏名称"
            />
          </el-form-item>
          <el-form-item
            label="游戏类型:"
            prop="type"
          >
            <el-input
              v-model="formData.type"
              :clearable="true"
              placeholder="请输入游戏类型"
            />
          </el-form-item>
          <el-form-item
            label="类型:"
            prop="develop_type"
          >
            <el-select
              v-model.number="formData.develop_type"
              placeholder="请选择类型"
              clearable
            >
              <el-option
                label="自研"
                :value="1"
              />
              <el-option
                label="非自研"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="formData.develop_type == 2"
            label="游戏厂商:"
            prop="cp_name"
          >
            <el-input
              v-model="formData.cp_name"
              :clearable="true"
              placeholder="请输入游戏厂商"
            />
          </el-form-item>
          <el-form-item
            label="原包名:"
            prop="origin_name"
          >
            <el-input
              v-model="formData.origin_name"
              :clearable="true"
              placeholder="请输入原包名"
            />
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button
            type="primary"
            @click="enterDialog"
          >确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import {
  createQmGame,
  deleteQmGame,
  updateQmGame,
  findQmGame,
  getQmGameList
} from '@/api/qmGame'
// 获取游戏列表

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict, ReturnArrImg, onDownloadFile } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'

defineOptions({
  name: 'QmAssetsGame'
})

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
  game_name: '',
  type: '',
  develop_type: '',
  cp_name: '',
  origin_name: ''
})

// 验证规则
const rule = reactive({
  game_name: [{
    required: true,
    message: '请输入游戏名称',
    trigger: ['input', 'blur'],
  },
  {
    whitespace: true,
    message: '不能只输入空格',
    trigger: ['input', 'blur'],
  }
  ],
  type: [{
    required: true,
    message: '请输入游戏类型',
    trigger: ['input', 'blur'],
  },
  {
    whitespace: true,
    message: '不能只输入空格',
    trigger: ['input', 'blur'],
  }
  ],
  develop_type: [{
    required: true,
    message: '请选择类型',
    trigger: ['input', 'blur'],
  }
  ],
  origin_name: [{
    required: true,
    message: '请输入原包名',
    trigger: ['input', 'blur'],
  }
  ],

})

const elFormRef = ref()
const elSearchFormRef = ref()

// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  elSearchFormRef.value?.validate(async(valid) => {
    if (!valid) return
    page.value = 1
    pageSize.value = 10
    getTableData()
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getQmGameList({ page: page.value, page_size: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list

    total.value = table.data.total
    page.value = table.data.page
  }
}

getTableData()

const platformOptions = ref([])
// 获取需要的字典 可能为空 按需保留
const setOptions = async() => {
  platformOptions.value = await getDictFunc('platform')
}

setOptions()

// 删除行
const deleteRow = (row) => {
  ElMessageBox.confirm('确定要删除吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteQmGameFunc(row)
  })
}

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateQmGameFunc = async(row) => {
  const res = await findQmGame(row.id)
  type.value = 'update'
  if (res.code === 0) {
    formData.value = res.data
    dialogFormVisible.value = true
  }
}

// 删除行
const deleteQmGameFunc = async(row) => {
  const res = await deleteQmGame(row.id)
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    if (tableData.value.length === 1 && page.value > 1) {
      page.value--
    }
    getTableData()
  }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
  type.value = 'create'
  dialogFormVisible.value = true
}

// 关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false
  formData.value = {
    game_name: '',
    type: '',
    develop_type: '',
    cp_name: '',
    origin_name: ''
  }
}
// 弹窗确定
const enterDialog = async() => {
  elFormRef.value?.validate(async(valid) => {
    if (!valid) return
    let res

    switch (type.value) {
      case 'create':
        res = await createQmGame(formData.value)
        break
      case 'update':
        res = await updateQmGame(formData.value)
        break
      default:
        res = await createQmGame(formData.value)
        break
    }
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '创建/更改成功'
      })
      closeDialog()
      getTableData()
    }
  })
}

</script>

<style>

</style>
